package com.zj.base.httpclien;

import com.zj.base.permission.ConfigurationModules;
import com.zj.base.permission.interfaces.OnPermissionCheckListener;
import com.zj.base.permission.manifests.PermissionsManifest;
import com.zj.base.utils.ToastUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhaojie on 2018/4/10.
 */

@SuppressWarnings("unused")
public class GetRequestBuilder<T> extends HttpRequestBuilder<T> {


    GetRequestBuilder(int reqCode, HttpClient<T> client) {
        super(reqCode, client);
    }

    @Override
    HttpMethod getMethod() {
        return HttpMethod.get;
    }

    /**
     * compare a default cookies in getDefaultCookie();
     */
    public GetRequestBuilder<T> cookie(Map<String, String> cookie) {
        client.setCookies(cookie);
        return this;
    }

    public GetRequestBuilder<T> url(String url) {
        client.setUrl(url);
        return this;
    }

    public GetRequestBuilder<T> timeOut(long t) {
        client.setTimeOut(t);
        return this;
    }

    public GetRequestBuilder<T> params(Map<String, String> params) {
        client.setParams(params);
        return this;
    }

    public void download(String path, String fileName) {
        client.setFileName(fileName);
        client.setDownloadPath(path);
        Map<String, Boolean> pers = new HashMap<>();
        pers.put(PermissionsManifest.READ_EXTERNAL_STORAGE, true);
        pers.put(PermissionsManifest.WEITE_EXTERNAL_STORAGE, true);
        client.getDataInterface().onPermissionsCheck(pers, new OnPermissionCheckListener() {
            @Override
            public void startCheck(ConfigurationModules modules) {
                ToastUtils.show("必要权限检测中，请稍候...");
            }

            @Override
            public void result(ConfigurationModules modules, State state) {

            }

            @Override
            public void onSuccess(List<ConfigurationModules> modulesList) {
                client.setType(HttpClient.TYPE.download);
                send();
            }
        });
    }
}
